// pages/myName/myName.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var userinfo;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    type: "",
    brief: ""
  },
  name: function (e) {
    // console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  brief: function (e) {
    console.log(e)
    this.setData({
      brief: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data;
      },
    })
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        userinfo = res.data;
      },
    })
    that.setData({
      type: options._type
    })
  },
  rename() {
    var that = this;
    var nickname = that.data.name;
    if (nickname.length == 0) {
      wx.showToast({
        title: '请输入新的昵称',
        icon: 'none',
      })
    } else {
      wx.request({
        url: url.URl + 'my/profile/nickname',
        data: { nickname: nickname },
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          console.log(res)
          var msg = res.data.msg
          if (res.data.code == 200) {
            wx.showToast({
              title: msg,
              success: function (res) {
                userinfo.nickname = nickname;
                wx.setStorage({
                  key: 'userinfo',
                  data: userinfo,
                })
                wx.navigateBack({
                  delta: 1,
                })
              },
            })
          } else {
            wx.showToast({
              title: msg,
              icon: "none",
              success: function (res) {

              },
            })
          }
        }
      });
    }

  },
  reIntro: function () {
    var that = this;
    var brief = that.data.brief;
    if (brief.length == 0) {
      wx.showToast({
        title: '请输入个人简介',
        icon: 'none',
      })
    } else {
      wx.request({
        url: url.URl + 'my/profile/brief',
        data: { brief: brief },
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          console.log(res)
          var msg = res.data.msg
          if (res.data.code == 200) {
            wx.showToast({
              title: msg,
              success: function (res) {


              },
            })
          } else {
            wx.showToast({
              title: msg,
              icon: "none",
              success: function (res) {

              },
            })
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})